<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="userRole" placeholder="请选择角色">
          <el-option value="0" label="普通用户"></el-option>
          <el-option value="1" label="一级会员"></el-option>
          <el-option value="2" label="二级会员"></el-option>
          <el-option value="3" label="三级会员"></el-option>
          <el-option value="4" label="代理商"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:user:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:user:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="用户手机号" align="center" prop="phone" />
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="用户角色" align="center" prop="userType">
        <template slot-scope="scope">
          <span v-if="scope.row.userType == 1">代理商</span>
          <span v-else-if="scope.row.userType == 0 && scope.row.level == 0"
            >普通用户</span
          >
          <span v-else-if="scope.row.userType == 0 && scope.row.level == 1"
            >一级会员</span
          >
          <span v-else-if="scope.row.userType == 0 && scope.row.level == 2"
            >二级会员</span
          >
          <span v-else-if="scope.row.userType == 0 && scope.row.level == 3"
            >三级会员</span
          >
        </template>
      </el-table-column>

      <el-table-column label="佣金金额" align="center" prop="brokeragePrice" />
      <el-table-column label="配额" align="center" prop="integral" />
      <el-table-column label="用户购买次数" align="center" prop="payCount" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:user:edit']"
            >设置</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户对话框 -->
    <el-dialog
      title="设为代理商"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="角色" prop=" integral">
          <el-radio-group v-model="form.userType">
            <el-radio label="0" border>普通用户</el-radio>
            <el-radio label="1" border>代理商</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="原配额" prop=" integral" v-if="form.userType == 1">
          <el-input v-model="form.integral" readonly />
        </el-form-item>
        <el-form-item label="配额" prop="addIntegral" v-if="form.userType == 1">
          <el-input-number
            v-model="form.addIntegral"
            placeholder="请输入配额"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  agentApi,
} from "@/api/business/user";

export default {
  name: "User",
  data() {
    return {
      userRole: null,
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      addIntegral: 0,
      // 用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        password: null,
        realName: null,
        birthday: null,
        cardId: null,
        mark: null,
        partnerId: null,
        groupId: null,
        nickname: null,
        avatar: null,
        phone: null,
        addIp: null,
        lastIp: null,
        nowMoney: null,
        brokeragePrice: null,
        integral: null,
        signNum: null,
        status: null,
        level: null,
        spreadUid: null,
        spreadTime: null,
        userType: null,
        isPromoter: null,
        payCount: null,
        spreadCount: null,
        addres: null,
        adminid: null,
        loginType: null,
        wxProfile: null,
        levelTime: null,
        isUser: false,
      },
      // 表单参数
      form: {
        addIntegral: 0,
      },
      // 表单校验
      rules: {
        addIntegral: [
          { required: true, message: "请输入配额", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      console.log(123, this.userRole);
      this.loading = true;
      if (this.userRole == 4) {
        this.queryParams.userType = 1;
        this.queryParams.level = 0;
      } else {
        if (this.userRole != null) {
          this.queryParams.level = this.userRole;
          this.queryParams.userType = 0;
        } else {
          this.queryParams.level = "";
          this.queryParams.userType = "";
        }
      }
      listUser(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uid: null,
        username: null,
        password: null,
        realName: null,
        birthday: null,
        cardId: null,
        mark: null,
        partnerId: null,
        groupId: null,
        nickname: null,
        avatar: null,
        phone: null,
        addIp: null,
        lastIp: null,
        nowMoney: null,
        brokeragePrice: null,
        integral: null,
        signNum: null,
        status: 0,
        level: null,
        spreadUid: null,
        spreadTime: null,
        userType: null,
        isPromoter: null,
        payCount: null,
        spreadCount: null,
        addres: null,
        adminid: null,
        loginType: null,
        wxProfile: null,
        levelTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.userRole = null;
      console.log(55, this.userRole);
      this.queryParams.level = "";
      this.queryParams.userType = "";
      this.resetForm("queryForm");
      console.log(55, this.userRole);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.uid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uid = row.uid || this.ids;
      getUser(uid).then((response) => {
        this.form = response.data;
        this.isUser = this.form.userType === "1" ? true : false;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.isUser);

          console.log(this.form.userType);
          if (this.isUser && this.form.userType === "1") {
            updateUser({
              uid: this.form.uid,
              addIntegral: this.form.addIntegral,
            }).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            agentApi({
              uid: this.form.uid,
              userType: this.form.userType,
              addIntegral: this.form.addIntegral,
            }).then((response) => {
              this.$modal.msgSuccess("设置成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uids = row.uid || this.ids;
      this.$modal
        .confirm('是否确认删除用户编号为"' + uids + '"的数据项？')
        .then(function () {
          return delUser(uids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/user/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
